package com.baidu.elinkagescroll.sample;

import com.baidu.elinkagescroll.ResourceTable;

import ohos.agp.colors.RgbColor;
import ohos.agp.components.*;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.utils.Color;
import ohos.app.Context;

import java.util.ArrayList;

/**
 * description :
 *
 * @since 2021/7/15
 */
public class LinkageRecyclerAdapter extends BaseItemProvider {
    private Context mContext;
    private ArrayList<String> mList;
    private String mColor;

    /**
     * init LinkageRecyclerAdapter
     *
     * @param context context
     * @param list list
     * @param color color
     */
    public LinkageRecyclerAdapter(Context context, ArrayList<String> list, String color) {
        mContext = context;
        mList = list;
        mColor = color;
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public Component getComponent(int position, Component component, ComponentContainer componentContainer) {
        if (component == null) {
            component = LayoutScatter.getInstance(mContext)
                    .parse(ResourceTable.Layout_linkage_rv_sample_item, null, false);
            ShapeElement element = new ShapeElement();
            element.setRgbColor(RgbColor.fromArgbInt(Color.getIntColor(mColor)));
            component.setBackground(element);
        }
        Text title = (Text) component.findComponentById(ResourceTable.Id_title);
        title.setText(mList.get(position));
        return component;
    }
}
